package com.itheima.info.manager.system.controller;

import com.itheima.info.manager.system.domain.Teacher;
import com.itheima.info.manager.system.service.TeacherService;

import java.util.Scanner;

public class TeacherController {
    private TeacherService teacherService = new TeacherService();
    private Scanner sc = new Scanner(System.in);

    public void Teacherstart() {

        teacherLoop:
        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    //System.out.println("添加老师");
                    addTeacher();
                    break;
                case "2":
                    //System.out.println("删除老师");
                    deleteTeacher();
                    break;
                case "3":
                    //System.out.println("修改老师");
                    updateTeacher();
                    break;
                case "4":
                    // System.out.println("查看老师");
                    findAllTeachers();
                    break;
                case "5":
                    System.out.println("感谢您使用老师管理系统, 再见!");
                    break teacherLoop;
                default:
                    System.out.println("您的输入有误, 请重新输入");
                    break;
            }
        }
    }

    //查看老师
    private void findAllTeachers() {
        Teacher[] teachers = teacherService.findAllTeachers();
        if (teachers == null) {
            System.out.println("没有信息，请先添加");
            return;
        }
        System.out.println("工号\t姓名\t年龄\t生日");
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
            if (teacher != null) {
                System.out.println(teacher.getSid() + teacher.getName() + teacher.getAge() + teacher.getBirthday());
            }
        }
    }

    //修改老师
    private void updateTeacher() {
        String sid = lurugonghao();
        Teacher teacher = luruTeacherInfo(sid);
        teacherService.updateTeacher(sid, teacher);
        System.out.println("修改成功");
    }

    //删除老师
    public void deleteTeacher() {
        String sid = lurugonghao();
        teacherService.deleteTeacher(sid);
        System.out.println("删除成功");
    }

    //键盘录入
    public String lurugonghao() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入工号");
        String sid;
        while (true) {
            sid = sc.next();
            boolean exists = teacherService.isExists(sid);
            if (exists) {
                break;
            } else {
                System.out.println("不存在，请重新录入");
            }
        }
        return sid;
    }

    //添加老师
    private void addTeacher() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入工号");
        String sid;
        while (true) {
            sid = sc.next();
            boolean exists = teacherService.isExists(sid);
            if (exists) {
                System.out.println("已经存在，请重新录入");
            } else {
                break;
            }
        }
        Teacher teacher = luruTeacherInfo(sid);
        boolean b = teacherService.addTeacher(teacher);
        if (b) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    private Teacher luruTeacherInfo(String sid) {
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        String age = sc.next();
        System.out.println("请输入生日");
        String birthday = sc.next();
        Teacher teacher = new Teacher();
        teacher.setSid(sid);
        teacher.setName(name);
        teacher.setAge(age);
        teacher.setBirthday(birthday);
        return teacher;
    }
}

